# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:
from base_class import BaseClass


class GetReplace(BaseClass):

    def __init__(self, **kwargs):
        self.db_config = kwargs.get("db_config")
        self.insert_table = kwargs.get("insert_table")
        self.conn, self.curs = self.get_conn_curs(db_config=self.db_config, type='dict')
        self.num = 0

    def get_replace_info(self):
        sql = u'''select distinct trim(F5), trim(F11) from catalog where trim(F11)!="";'''
        self.curs.execute(sql)
        data = self.curs.fetchall()
        return data

    def get_replace_raw(self):
        replace_info = self.get_replace_info()
        for i in replace_info:
            pid = i.get("trim(F5)")
            pid1 = i.get("trim(F11)")
            _dict = dict(
                pid=pid,
                pid1=pid1
            )
            insert_sql = self.construct_insert_sql(self.insert_table, _dict)
            self.curs.execute(insert_sql)
            self.num += 1
            if self.num % 1000 == 0:
                print("提交insert缓存, 当前数量: {}".format(self.num))
                self.conn.commit()
        self.conn.commit()
        print("最终提交, 总数据量: {}".format(self.num))

    def get_replace_raw_info(self):
        sql = '''select `cid`,`brandCode`,`pid`,`label`,`quantity`,`rid` from `cars_part_num` where rid!=""'''
        self.curs.execute(sql)
        data = self.curs.fetchall()
        return data

    def get_label_dict(self):
        sql = u'''select distinct `pid`,`label` from `cars_part_num`'''
        self.curs.execute(sql)
        data = self.curs.fetchall()
        _dict = {i.get("pid"): i.get("label") for i in data}
        return _dict

    def get_replace(self):
        # 获取原始替换件数据
        replace_raw_info = self.get_replace_raw_info()
        # 获取label对照字典
        # label_dict = self.get_label_dict()
        _dict = {}
        for i in replace_raw_info:
            cid = i.get("cid")
            label = i.get("label")
            quantity = i.get("quantity")
            brandCode = i.get("brandCode")
            pid = i.get("pid")
            rid = i.get("rid")
            if pid in _dict or rid in _dict:  # 如果有任何一个pid在字典中,就取出对应的sid, 并且添加key
                sid = _dict.get(pid, _dict.get(rid))
                _dict[pid] = sid
                _dict[rid] = sid
            else:
                _dict[pid] = pid
                _dict[rid] = pid
                sid = pid

            # 主要逻辑已通顺,现有数据判断其brandCode有些困难,下次跑part_num表的时候记得把rid跑出来
            data_dict = dict(
                sid=sid,
                pid=pid,
                rid=rid,
                label=label,
                ptype='',
                desc='',
                qty=quantity,
                channel='',
                name='',
                brandCode=brandCode,
                createtime='now()',
            )
            sql_tail = 'on duplicate key update `brandCode`=values(`brandCode`)'
            insert_sql = self.construct_insert_sql('parts_replace_info', data_dict)
            self.curs.execute(insert_sql+sql_tail)
            self.num += 1
            if self.num % 1000 == 0:
                self.conn.commit()
                print("提交insert缓存, 当前数量: {}".format(self.num))
        self.conn.commit()
        print("最终提交, 总数据量: {}".format(self.num))

    def run(self):
        # self.get_replace_raw()
        self.get_replace()

if __name__ == '__main__':
    db_config = dict(
        host="127.0.0.1",
        port=3306,
        user="root",
        passwd="123456",
        db="nissan_new",
        charset="utf8",
    )
    insert_table = 'parts_replace_info'
    tools = GetReplace(db_config=db_config, insert_table=insert_table)
    tools.run()